home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
ici
/
ici.cpi
/
control.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-27
|
4KB
|
189 lines
#include "exec.h"
#include "op.h"
#include "int.h"
#include "buf.h"
#include "pc.h"
#include "struct.h"
#include "null.h"
#include "forall.h"
#include "catch.h"
/*
* self => array looper &array[2] (xs)
* array => - (os)
*
* Ie, like a loop, but on the first time in it skips the first two elements
* of the the loop, which are expected to be an array of code for the step
* and an exec operator to run it.
*/
int
op_for()
{
pc_t *pc;
if ((pc = new_pc(arrayof(o_top[-1]))) == NULL)
return 1;
pc->pc_next += opof(x_top[-1])->op_code;
x_top[-1] = o_top[-1];
*x_top++ = objof(&o_looper);
*x_top++ = objof(pc);
--o_top;
return 0;
}
/*
* obj self => obj self pc (xs)
* => (os)
*/
int
op_looper()
{
if ((*x_top = objof(new_pc(arrayof(x_top[-2])))) == NULL)
return 1;
++x_top;
return 0;
}
/*
* Pop the execution stack until a looper is found.
*/
STATIC int
op_continue()
{
object_t **s;
for (s = xs->a_top; s > xs->a_base + 1; --s)
{
if (iscatch(s[-1]) && catchof(s[-1])->c_catcher == objof(&o_null))
break;
if (s[-1] == objof(&o_looper) || isforall(s[-1]))
{
x_top = s;
return 0;
}
}
error = "continue not within loop";
return 1;
}
/*
* bool obj => bool (os) OR pc (xs)
*/
STATIC int
op_andand()
{
int c;
if ((c = !isfalse(o_top[-2])) == opof(x_top[-1])->op_code)
{
/*
* Have to test next part of the condition.
*/
if ((*--x_top = objof(new_pc(arrayof(o_top[-1])))) == NULL)
return 1;
o_top -= 2;
++x_top;
return 0;
}
/*
* Reduce the value to 0 or 1.
*/
o_top[-2] = objof(c ? o_one : o_zero);
--o_top;
--x_top;
return 0;
}
/*
* bool obj => -
*/
STATIC int
op_if()
{
if (isfalse(o_top[-2]))
{
o_top -= 2;
--x_top;
return 0;
}
if ((*--x_top = objof(new_pc(arrayof(o_top[-1])))) == NULL)
return 1;
++x_top;
o_top -= 2;
return 0;
}
/*
* bool obj1 obj2 => -
*/
STATIC int
op_ifelse()
{
--x_top;
if ((*x_top = objof(new_pc(arrayof(o_top[-1 - !isfalse(o_top[-3])])))) == NULL)
return 1;
++x_top;
o_top -= 3;
return 0;
}
/*
* NULL self (xs) =>
*/
STATIC int
op_switcher()
{
x_top -= 2;
return 0;
}
/*
* value array struct => (os)
* => NULL switcher (pc(array) + struct.value) (xs)
*/
STATIC int
op_switch()
{
register slot_t *sl;
if ((sl = find_raw_slot(structof(o_top[-1]), o_top[-3]))->sl_key == NULL)
{
if ((sl = find_raw_slot(structof(o_top[-1]), objof(&o_mark)))->sl_key == NULL)
{
o_top -= 3;
--x_top;
return 0;
}
}
x_top[-1] = objof(&o_null);
*x_top++ = objof(&o_switcher);
if ((*x_top = objof(new_pc(arrayof(o_top[-2])))) == NULL)
return 1;
pcof(*x_top)->pc_next += intof(sl->sl_value)->i_value;
++x_top;
o_top -= 3;
return 0;
}
STATIC int
op_pop()
{
--o_top;
--x_top;
return 0;
}
op_t o_exec = {OBJ(TC_OP, op_type), NULL, OP_EXEC};
op_t o_looper = {OBJ(TC_OP, op_type), op_looper};
op_t o_loop = {OBJ(TC_OP, op_type), NULL, OP_LOOP};
op_t o_break = {OBJ(TC_OP, op_type), NULL, OP_BREAK};
op_t o_continue = {OBJ(TC_OP, op_type), op_continue};
op_t o_if = {OBJ(TC_OP, op_type), op_if};
op_t o_ifnotbreak = {OBJ(TC_OP, op_type), NULL, OP_IFNOTBREAK};
op_t o_ifelse = {OBJ(TC_OP, op_type), op_ifelse};
op_t o_pop = {OBJ(TC_OP, op_type), op_pop};
op_t o_andand = {OBJ(TC_OP, op_type), op_andand, 0, 1};
op_t o_barbar = {OBJ(TC_OP, op_type), op_andand, 0, 0};
op_t o_switch = {OBJ(TC_OP, op_type), op_switch};
op_t o_switcher = {OBJ(TC_OP, op_type), op_switcher};